package com.hugehard.here;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class IntroductionViewPager extends Activity {
	private ViewPager introductionViewPager;
	private ArrayList<View> introductionPages;
	private ImageView dotView;
	private ImageView[] dotViews;
	private ViewGroup introductionPagesViewGroup;
	private ViewGroup pagesDotsGroup;
	private Button finishIntroBtn;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	            WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		LayoutInflater inflater = getLayoutInflater();
		
		View page0 = inflater.inflate(R.layout.introduction_p3, null);
		if (page0 != null) {
			ImageView iv = (ImageView) page0.findViewById(R.id.introduce_img);
			iv.setImageResource(R.drawable.intro_p0);
			View btn = page0.findViewById(R.id.finish_intro_btn);
			btn.setVisibility(View.INVISIBLE);
		}
		
		View page1 = inflater.inflate(R.layout.introduction_p3, null);
		if (page1 != null) {
			ImageView iv = (ImageView) page1.findViewById(R.id.introduce_img);
			iv.setImageResource(R.drawable.intro_p1);
			View btn = page1.findViewById(R.id.finish_intro_btn);
			btn.setVisibility(View.INVISIBLE);
		}
		
		View page2 = inflater.inflate(R.layout.introduction_p3, null);
		if (page2 != null) {
			ImageView iv = (ImageView) page2.findViewById(R.id.introduce_img);
			iv.setImageResource(R.drawable.intro_p2);
			View btn = page2.findViewById(R.id.finish_intro_btn);
			btn.setVisibility(View.INVISIBLE);
		}
		
		View page3 = inflater.inflate(R.layout.introduction_p3, null);
		if (page3 != null) {
			ImageView iv = (ImageView) page3.findViewById(R.id.introduce_img);
			iv.setImageResource(R.drawable.intro_p3);
			View btn = page3.findViewById(R.id.finish_intro_btn);
			btn.setVisibility(View.VISIBLE);
		}

		introductionPages = new ArrayList<View>();
		introductionPages.add(page0);
		introductionPages.add(page1);
		introductionPages.add(page2);
		introductionPages.add(page3);

		dotViews = new ImageView[introductionPages.size()];
		introductionPagesViewGroup = (ViewGroup) inflater.inflate(
				R.layout.introduction_pages, null);

		pagesDotsGroup = (ViewGroup) introductionPagesViewGroup
				.findViewById(R.id.page_dot);
		introductionViewPager = (ViewPager) introductionPagesViewGroup
				.findViewById(R.id.introduction_page);

		for (int i = 0; i < introductionPages.size(); i++) {
			dotView = new ImageView(IntroductionViewPager.this);
			dotView.setLayoutParams(new LayoutParams(20, 20));
			dotView.setPadding(20, 0, 20, 0);
			dotViews[i] = dotView;
			if (i == 0) {
				dotViews[i]
						.setBackgroundResource(R.drawable.page_indicator_focused);
			} else {
				dotViews[i].setBackgroundResource(R.drawable.page_indicator);
			}
			pagesDotsGroup.addView(dotViews[i]);
		}

		setContentView(introductionPagesViewGroup);

		finishIntroBtn = (Button) page3.findViewById(R.id.finish_intro_btn);
		finishIntroBtn.setOnClickListener(new FinishBtnOnClickListener());

		introductionViewPager.setAdapter(new GuidePageAdapter());
		introductionViewPager
				.setOnPageChangeListener(new GuidePageChangeListener());
	}

	private class FinishBtnOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(IntroductionViewPager.this,
					MainActivity.class);
			startActivity(intent);
			finish();
		}
	}

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return introductionPages.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(introductionPages.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(introductionPages.get(arg1));
			return introductionPages.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < dotViews.length; i++) {
				dotViews[arg0]
						.setBackgroundResource(R.drawable.page_indicator_focused);
				if (arg0 != i) {
					dotViews[i]
							.setBackgroundResource(R.drawable.page_indicator);
				}
			}
		}
	}
}